<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 16:28
 */
class Service_Order{

    /**
     * 生成订单号
     * @return string
     */
    public static function getOrderNo(){
        $ddNumber = date("YmdHis") . mt_rand(10000, 99999);
        return $ddNumber;
    }

    /**
     * 获取订单数量
     * @param $uid
     * @param $status
     * @return mixed
     */
    public static function getOrderNum($uid,$status=''){
        $ordersT = new FTable('orders','','xhs_slave');
        $where['uid'] = $uid;
        if($status !== '') $where['status'] = $status;
        return $ordersT->where($where)->count();
    }

    //获取订单状态
    public static function getOrderStatusName($status){
        switch($status){
            case 1:
                $status_name="待付款";
                break;
            case 2:
                $status_name="待接单";
                break;
            case 3:
                $status_name="商家已接单";
                break;
            case 4:
                $status_name="派送中";
                break;
            case 5:
                $status_name="已完成";
                break;
            case 6:
                $status_name="已取消";
                break;
            default:
                $status_name="全部";
        }
        return $status_name;
    }

    /**
     * 设置未提醒订单队列
     * @param $order_no
     * @param $shop_id
     */
    public static function orderPushQueue($order_no,$shop_id){
        if($order_no && $shop_id){
            $list = "all_order_list";
            $shop_list = "order_list_" . $shop_id;
            $redis = FRedis::getInstance(26, 'redis_config_87');
            $redis->selectDb(26);
            $redis->listPush($list,$order_no);
            $redis->listPush($shop_list,$order_no);
        }
    }

    /**
     * 获取未提醒订单
     * @param string $shop_id
     * @return array
     */
    public static function orderPopQueue($shop_id = ''){
        if($shop_id > 0){
            $list = "order_list_" . $shop_id;
        }else{
            $list = "all_order_list";
        }
        $redis = FRedis::getInstance(26, 'redis_config_87');
        $redis->selectDb(26);
        $size = $redis->listSize($list);
        $redis->delete($list);
        return array('size'=>$size);
    }

    //获取订单列表
    public static function getOrderList($shopId,$status,$page=0){
        $orderModel = new FTable("orders",'','xhs_slave');
        if(!empty($page)){
            return $orderModel->where(array('shop_id'=>$shopId,'status'=>$status))->order("create_time desc")->limit(10)->page($page)->select();
        }else{
            return $orderModel->where(array('shop_id'=>$shopId,'status'=>$status))->order("create_time desc")->select();
        }
    }
    //取消订单
    public static function cancelOrder($oid){
        $orderModel = new FTable("orders",'','xhs_slave');
        $orderInfo = $orderModel->where(array('auto_id'=>$oid))->find();
        $res=Service_ApiBaidu::cancelOrder(array('order_id'=>$orderInfo['o_id'],'reason'=>'admin cancel','ship_id'=>$orderInfo['ship_id']));
        return $res;
    }
    //变更记录消耗
    public static function updateExpend($status,$oid){
        $orderModel = new FTable("orders",'','xhs_master');
        $orderInfo = $orderModel->where(array('auto_id'=>$oid))->find();
        if($status == 3){
            $diff=time()-strtotime($orderInfo['pay_time']);
            $orderModel->update1(array('status'=>$status,'recv_expend'=>$diff),array('auto_id'=>$oid));
        }elseif($status == 4){
            $diff=time()-strtotime($orderInfo['pay_time']);
            $makediff=$diff-$orderInfo['recv_expend'];
            $orderModel->update1(array('status'=>$status,'make_expend'=>$makediff),array('auto_id'=>$oid));
        }elseif($status == 5){
            $diff=time()-strtotime($orderInfo['pay_time']);
            $senddiff=$diff-($orderInfo['recv_expend']+$orderInfo['make_expend']);
            $orderModel->update1(array('status'=>$status,'complete_time'=>date('Y-m-d H:i:s'),'send_expend'=>$senddiff),array('auto_id'=>$oid));
        }else{
            $orderModel->update1(array('status'=>$status),array('auto_id'=>$oid));
        }
    }

    /**
     * 是否首单
     * @param $uid
     * @return bool
     */
    public static function firstOrder($uid){
        $order = FDB::fetchFirst("SELECT auto_id FROM orders WHERE uid={$uid} AND status IN(2,3,4,5)",'xhs_slave');
        if(empty($order))
            return true;
        else
            return false;
    }

    //通用配置
    public static function Peizhi(){
        $key = 'peizhi_xhs_price_0818';
        $cacheData = FCache::get($key);
        if($cacheData){
            return $cacheData;
        }else{
            $table = new FTable('peizhi', '', 'xhs_slave');
            $id=1;
            $data = $table->where(array('id' => $id))->find();
            if($data){
                FCache::set($key, $data, 30*24*60);
                return $data;
            }else{
                return false;
            }
        }
    }

    /**
     * 清除通用配置缓存
     */
    public static function clearPeizhiCache(){
        $key = 'peizhi_xhs_price_0818';
        FCache::delete($key);
    }

    public static function timeFormat($times)
    {
        if ($times) {
            $H = date('H', $times) - 8;
            $i = date('i', $times);
            $s = date('s', $times);

            if($times<3600){
                return "{$i}分钟{$s}秒";
            }else{
                return "{$H}小时{$i}分钟{$s}秒";
            }
        }else{
            return false;
        }
    }
}